package com.itheima.utils;

import com.itheima.pojo.MethodCount;
import redis.clients.jedis.Jedis;

import java.util.*;

public class MethodCountUtils {

    public static void methodCount(String methodName)  {
        String date = null;
        try {
            date = DateUtils.parseDate2String(DateUtils.getToday());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Jedis jedis = new Jedis("127.0.0.1",6379);

        Double zscore = jedis.zscore(date, methodName);
        if (zscore==null){
            zscore=1.0;
        }else {
            zscore++;
        }
        jedis.zadd(date,zscore,methodName);
        jedis.close();
    }
    public static List getMethodCount(String date){
        Jedis jedis = new Jedis("127.0.0.1",6379);
        Set<String> methodNames = jedis.zrange(date, 0, -1);
        List list = new ArrayList<>();

        for (String methodName : methodNames) {
            int i = new Double(jedis.zscore(date, methodName)).intValue();
            MethodCount methodCount = new MethodCount(methodName,i);
            list.add(methodCount);
        }
        jedis.close();
        return list;
    }

}
